package dao

import (
	"device_srv/device"
	"device_srv/models"
	"encoding/json"
	"errors"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"smart-health-platforms/config"
	"smart-health-platforms/pkg"
	"time"
)

// 设备
type DevicesDao struct {
	db *gorm.DB
}

// DevicesAdd 设备添加
func DevicesAdd(in *device.DevicesAddRequest) (*models.Devices, error) {
	//接收参数
	devices := models.Devices{
		BrandId:           uint(in.BrandId),
		DeviceName:        in.DeviceName,
		DeviceModel:       in.DeviceModel,
		DeviceDescription: in.DeviceDescription,
		PurchaseDate:      in.PurchaseDate,
		Status:            uint(in.Status),
	}
	err := devices.CreateDevices()
	pkg.Info("设备添加成功",
		zap.Int("id", int(devices.Id)),
		zap.String("DeviceName", devices.DeviceName),
	)
	if err != nil {
		pkg.Warn("设备信息添加失败")
		return nil, errors.New("设备信息添加失败")
	}
	return &devices, err

}

// DevicesDetail 设备详情

func DevicesDetail(in *device.DevicesDetailRequest) (*models.Devices, error) {
	//接收参数
	devices := models.Devices{
		Id: uint(in.Id),
	}

	err := devices.DevicesDetail()
	pkg.Info("设备详情获取成功",
		zap.Int("id", int(devices.Id)),
		zap.String("DeviceName", devices.DeviceName),
	)
	if err != nil {
		pkg.Warn("设备详情获取失败")
		return nil, errors.New("设备信息详情获取失败")
	}
	return &devices, err
}

// DevicesUpdate 设备修改
func DevicesUpdate(in *device.DevicesUpdateRequest) (*models.Devices, error) {
	//接收参数
	var devices models.Devices
	devices.Id = uint(in.Id)
	err := devices.GetDevicesById(in.Id)
	if err != nil {
		return nil, errors.New("设备ID查找失败")
	}
	if devices.DeviceName == "" {
		return nil, errors.New("该设备不存在，禁止修改信息")
	}
	devices.DeviceName = in.DeviceName
	devices.BrandId = uint(in.BrandId)
	devices.DeviceModel = in.DeviceModel
	devices.DeviceDescription = in.DeviceDescription
	devices.PurchaseDate = in.PurchaseDate
	devices.Status = uint(in.Status)
	err = devices.DevicesUpdate()
	pkg.Info("设备信息修改成功",
		zap.Int("id", int(devices.Id)),
		zap.String("DeviceName", devices.DeviceName),
	)
	if err != nil {
		pkg.Warn("设备信息修改失败")
		return nil, errors.New("设备信息修改失败")
	}
	return &devices, err
}

// DevicesDelete 设备删除
func DevicesDelete(in *device.DevicesDeleteRequest) (*models.Devices, error) {
	//接收参数
	devices := models.Devices{
		Id: uint(in.Id),
	}
	err := devices.GetDevicesById(in.Id)
	if err != nil {
		return nil, errors.New("设备ID查找失败")
	}
	if devices.Id == 0 {
		return nil, errors.New("该设备不存在，禁止删除")
	}
	err = devices.DevicesDelete()
	if err != nil {
		pkg.Warn("设备信息删除失败")
		return nil, errors.New("设备信息删除失败")
	}
	return &devices, err
}

// GetDevicesList 设备列表
func (dao *DevicesDao) GetDevicesList(DeviceName string) (devicesList []*models.Devices, err error) {
	err = config.DB.Where("device_name like ?", "%"+DeviceName+"%").Find(&devicesList).Error
	return
}
func DevicesList(in *device.DevicesListRequest) ([]*device.DevicesList, error) {
	val := config.Rdb.Get(config.Ctx, "DevicesLists").Val()
	var dev models.Devices
	devices, err := dev.GetDevicesList(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, errors.New("设备信息列表展示失败")
	}
	var devicesList []*device.DevicesList
	if val == "" {
		for _, dev := range devices {
			lists := device.DevicesList{
				Id:                int64(dev.Id),
				BrandId:           int64(dev.BrandId),
				DeviceName:        dev.DeviceName,
				DeviceModel:       dev.DeviceModel,
				DeviceDescription: dev.DeviceDescription,
				PurchaseDate:      dev.PurchaseDate,
				Status:            int64(dev.Status),
			}
			devicesList = append(devicesList, &lists)
		}
		//从数据库获取
		marshal, _ := json.Marshal(devicesList)
		config.Rdb.Set(config.Ctx, "DevicesLists", marshal, time.Minute*15)
	} else {
		//从缓存中获取
		_ = json.Unmarshal([]byte(val), &devicesList)

	}
	
	return devicesList, err
}
